package com.bj58.mis.apm.server.kylinreceive.repository;

import com.bj58.mis.apm.server.kylinreceive.model.KylinStorage;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * kylin项目对象操作jpa
 * @author wangxw
 */
public interface StorageRepository extends JpaRepository<KylinStorage, Long> {

    KylinStorage getByCubeIdAndRecordDate(String cubeId, Date recordDate );

    @Query(value = "select max(record_date) from kylin_storage ", nativeQuery = true)
    Date queryLastRecordDate();

    @Transactional
    @Modifying
    @Query(value = "insert into kylin_storage(cube_id, meta_data_size, hbase_data_size, record_date) " +
            "select cube_id, meta_data_size, hbase_data_size, :recordDate from kylin_storage where record_date = (select max(record_date) from kylin_storage)", nativeQuery = true)
    void updateRecords(@Param("recordDate") Date recordDate);
}
